const axios = require('axios');

// 测试API端点
const API_BASE_URL = 'http://localhost:3000/api/users';

// 测试用户数据
const testUser = {
  username: 'testuser_with_phone',
  password_hash: 'hashed_password_123',
  nickname: 'Test User With Phone',
  phone_number: '13800138000'
};

async function testPhoneNumberField() {
  try {
    // 1. 创建用户
    console.log('Creating user with phone number...');
    const createResponse = await axios.post(API_BASE_URL, testUser);
    console.log('Create user response:', createResponse.data);
    
    const userId = createResponse.data.id;
    
    // 2. 获取用户
    console.log('\nGetting user by ID...');
    const getResponse = await axios.get(`${API_BASE_URL}/${userId}`);
    console.log('Get user response:', getResponse.data);
    
    // 3. 更新用户
    console.log('\nUpdating user phone number...');
    const updatedUser = {
      ...testUser,
      phone_number: '13900139000',
      nickname: 'Updated Test User'
    };
    const updateResponse = await axios.put(`${API_BASE_URL}/${userId}`, updatedUser);
    console.log('Update user response:', updateResponse.data);
    
    // 4. 获取所有用户
    console.log('\nGetting all users...');
    const getAllResponse = await axios.get(API_BASE_URL);
    console.log('Get all users response:');
    getAllResponse.data.forEach(user => {
      console.log(`- User ${user.username}: ${user.phone_number}`);
    });
    
    // 5. 删除用户
    console.log('\nDeleting user...');
    await axios.delete(`${API_BASE_URL}/${userId}`);
    console.log('User deleted successfully');
    
    console.log('\nAll tests passed!');
  } catch (error) {
    console.error('Test failed:', error.response ? error.response.data : error.message);
  }
}

testPhoneNumberField();
